﻿using System.Timers;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using Tedds_Tool;
using LEET.CommonInterfaces;
using CommonInterfaces.Models;
using StandardScriptItems.SimpleControls;

namespace GUI.Custom_Classes
{
    /// <summary>
    /// Interaction logic for SI_Verify.xaml
    /// </summary>
    public partial class SI_Verify : SI_Base
    {
        #region Attributes

        IElementState state;

        ComboBox TypeBox;
        ElementProperySelector PropertySelector;
        ComboBox AssertBox;
        TextBox ValueBox;


        #endregion

        public SI_Verify(Nil triggerUpdate, IElementState m)
            : base(triggerUpdate)
        {
            InitializeComponent();
            InitializeWritenComponents(m);

            TypeBox.SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
            {
                triggerUpdate();
            };

            PropertySelector.PropertyChanged += delegate(String prop, String val)
            {
                ValueBox.Text = "\"" + val + "\"";
                triggerUpdate();
            };

            AssertBox.SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
            {
                if (args.AddedItems.Count > 0)
                {
                    if (args.AddedItems[0].ToString().Contains("Is True") || args.AddedItems[0].ToString().Contains("Is False"))
                    {
                        ValueBox.Visibility = System.Windows.Visibility.Hidden;
                        PropertySelector.PropertyBox.IsEnabled = true;
                    }
                    else if (args.AddedItems[0].ToString().Contains("Process Exists"))
                    {
                        ValueBox.Visibility = System.Windows.Visibility.Hidden;
                        PropertySelector.PropertyBox.IsEnabled = false;
                    }
                    else
                    {
                        ValueBox.Visibility = System.Windows.Visibility.Visible;
                        PropertySelector.PropertyBox.IsEnabled = true;
                    }
                }
                triggerUpdate();
            };

            ValueBox.TextChanged += delegate(object sender, TextChangedEventArgs args)
            {
                triggerUpdate();
            };

            
        }

        private void InitializeWritenComponents(IElementState m)
        {
            TypeBox = new ComboBox();
            TypeBox.Name = "TypeBox";
            ComboBoxItem elementType = new ComboBoxItem();
            elementType.Content = "ELEMENT";
            elementType.IsSelected = true;
            TypeBox.Items.Add(elementType);
            Grid.SetColumn(TypeBox, 0);
            
            PropertySelector = new ElementProperySelector(m);
            Grid.SetColumn(PropertySelector, 1);

            AssertBox = new ComboBox();
            AssertBox.Name = "AssertBox";
            ComboBoxItem Is = new ComboBoxItem();
            Is.IsSelected = true;
            Is.Content = "Is";
            ComboBoxItem IsNot = new ComboBoxItem();
            IsNot.Content = "Is Not";
            ComboBoxItem Contains = new ComboBoxItem();
            Contains.Content = "Contains";
            ComboBoxItem IsTrue = new ComboBoxItem();
            IsTrue.Content = "Is True";
            ComboBoxItem IsFalse = new ComboBoxItem();
            IsFalse.Content = "Is False";
            ComboBoxItem ProcessExists = new ComboBoxItem();
            ProcessExists.Content = "Process Exists";

            AssertBox.Items.Add(Is);
            AssertBox.Items.Add(IsNot);
            AssertBox.Items.Add(IsTrue);
            AssertBox.Items.Add(IsFalse);
            AssertBox.Items.Add(Contains);
            AssertBox.Items.Add(ProcessExists);
            Grid.SetColumn(AssertBox, 2);

            ValueBox = new TextBox();
            ValueBox.Name = "ValueBox";
            ValueBox.Text = "\"\"";
            Grid.SetColumn(ValueBox, 3);

            Griddy.Children.Add(TypeBox);
            Griddy.Children.Add(PropertySelector);
            Griddy.Children.Add(AssertBox);
            Griddy.Children.Add(ValueBox);
        }

        public override string ToString() 
        {
            return (TypeBox.SelectedItem as ComboBoxItem).Content as String + " | " +
                PropertySelector.ToString() + " | " +
                (AssertBox.SelectedItem as ComboBoxItem).Content as String + " | " +
                ValueBox.Text;
        }

    }
}